//
//  KLMDeviceNameAndTypePopViewController.swift
//  KLM
//
//  Created by 朱雨 on 2022/7/1.
//

import UIKit
import nRFMeshProvision

class KLMDeviceNameAndTypePopViewController: UIViewController {
    
    typealias TypeBlock = (_ type: Int) -> Void
    typealias NameBlock = (_ name: String) -> Void
    typealias SKUBlock = (_ sku: String?) -> Void
    
    @IBOutlet weak var contentView: UIView!
    @IBOutlet weak var categoryLab: UILabel!
    @IBOutlet weak var textField: CMTextField!
    @IBOutlet weak var categoryView: UIView!
    
    @IBOutlet weak var useOccationBgView: UIView!
    @IBOutlet weak var useOccationHeightConstraite: NSLayoutConstraint!
    
    @IBOutlet weak var titleLab: UILabel!
    
    @IBOutlet weak var useOccasionLab: UILabel!
    
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var confirmBtn: UIButton!
    
    //商品条码
    @IBOutlet weak var SKUView: UIView!
    @IBOutlet weak var SKUField: UITextField!
    @IBOutlet weak var scanBtn: UIButton!
    @IBOutlet weak var SKUHeightConstraint: NSLayoutConstraint!
    
    //设备名称
    var name: String?
    var useOccation: Int?
        
    var nameBlock: NameBlock?
    var typeBlock: TypeBlock?
    var skuBlock: SKUBlock?
    
    var cancelBlock: (() -> Void)?

    //选择的场合
    var selectCategory: KLMType?
    
    var isHideSkuView: Bool = true
    var isHideUseOccasionBgView: Bool = true
    ///是否是生鲜灯
    var isFresh = false
    
    var deviceType: nodeDeviceType?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        textField.limitTextLeght = 32

        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: textField.height))
        textField.leftView  = leftView
        textField.leftViewMode = .always
        textField.layer.cornerRadius = 4
        
<<<<<<< HEAD
<<<<<<< HEAD
        textField.backgroundColor = rgba(238, 239, 243, 1)
        
=======
>>>>>>> c9ff26f7439158038ece58549346ff469099cbd4
        contentView.layer.cornerRadius = 8
                
        //是否显示条码扫描
        SKUView.isHidden = isHideSkuView
        useOccationBgView.isHidden = isHideUseOccasionBgView
        if isHideSkuView {
            SKUHeightConstraint.constant = 0
        }
        if isHideUseOccasionBgView {
            useOccationHeightConstraite.constant = 0
        }
        
        ///国际化
        titleLab.text = KLMLocalizable.LANGLOC("Name the device")
        textField.placeholder = KLMLocalizable.LANGLOC("Please enter a name")
        useOccasionLab.text = KLMLocalizable.LANGLOC("Select Product Type")
        cancelBtn.setTitle(KLMLocalizable.LANGLOC("Cancel"), for: .normal)
        confirmBtn.setTitle(KLMLocalizable.LANGLOC("Confirm"), for: .normal)
<<<<<<< HEAD
<<<<<<< HEAD
        confirmBtn.configStyle(titleColor: appMainThemeColor)
=======
>>>>>>> c9ff26f7439158038ece58549346ff469099cbd4
        SKUField.placeholder = KLMLocalizable.LANGLOC("Scan or enter product barcode")
        
        contentView.backgroundColor = appBackGroupColor
        useOccationBgView.backgroundColor = appBackGroupColor
//        if apptype == .targetUnibright {
//            titleLab.textColor = AppTextColor
//            textField.placeHolderColor = AppPlaceHolderColor
//            textField.textColor = AppTextColor
//            textField.backgroundColor = unibrightViewBgColor
//            useOccasionLab.textColor = AppTextColor
//            categoryLab.textColor = AppTextColor
//            categoryView.backgroundColor = unibrightViewBgColor
//            cancelBtn.setTitleColor(AppTextColor, for: .normal)
//            confirmBtn.setTitleColor(AppTextColor, for: .normal)
//            SKUField.backgroundColor = unibrightViewBgColor
//            SKUField.textColor = AppTextColor
//            SKUField.placeHolderColor = AppPlaceHolderColor
//        }
        
        setupData()
    }
    
    private func setupData() {
                
        textField.text = name
        
        ///默认服装百货
        var index = 2
        if isFresh { ///生鲜默认是水果
            index = 5
        }
        if let useOccation = useOccation, useOccation > 0 {
            index = useOccation
        }
//        KLMUseOccasionManager.shared.getAllList(deviceType: deviceType)
//        if let category = KLMUseOccasionManager.shared.getType(num: index) {
//            categoryLab.text = KLMLocalizable.LANGLOC(category.title)
//            selectCategory = category
//        }
    }
    
    @IBAction func sure(_ sender: Any) {

        guard let text = KLMTool.isEmptyString(string: textField.text) else {
            SVProgressHUD.showInfo(withStatus: textField.placeholder)
            return
        }
        
        ///检查是否重名
        if let network = MeshNetworkManager.instance.meshNetwork {
            
            let notConfiguredNodes = network.nodes.filter({ !$0.isConfigComplete && !$0.isProvisioner})
            if notConfiguredNodes.contains(where: {$0.name == text}) {
                SVProgressHUD.showInfo(withStatus: KLMLocalizable.LANGLOC("The name already exists"))
                return
            }
        }
        
        if isHideUseOccasionBgView == false {
            
            guard let cate = selectCategory  else {
                SVProgressHUD.showInfo(withStatus: KLMLocalizable.LANGLOC("Select Product Type"))
                return
            }
            if let typeB = typeBlock {
                typeB(cate.num)
            }
        }
        
        if isHideSkuView == false {
            if let sku = skuBlock {
                sku(SKUField.text)
            }
        }
        
        if let nameB = nameBlock {
            nameB(text)
        }
        
        dismiss(animated: true, completion: nil)
    }
    
    @IBAction func tapCategory(_ sender: UITapGestureRecognizer) {
        
        let menuViewrect: CGRect = categoryView.convert(categoryView.bounds, to: KLMKeyWindow)
        let point: CGPoint = CGPoint.init(x: menuViewrect.origin.x, y: menuViewrect.origin.y + menuViewrect.size.height)
        KLMUseOccasionManager.shared.getAllList(deviceType: deviceType)
        KLMUseOccasionManager.shared.showDropView(point: point, isFresh: isFresh) {[weak self] type in
            guard let self = self else {return }
            self.selectCategory = type
            self.categoryLab.text = KLMLocalizable.LANGLOC(type.title)
        }
    }
    
    @IBAction func scan(_ sender: Any) {
        
//        KLMPhotoManager().photoAuthStatus { [weak self] in
//            guard let self = self else { return }
//            
//            let vc = KLMScanViewController()
//            vc.skuBlock = { [weak self] sku in
//                guard let self = self else { return }
//                self.SKUField.text = sku
//            }
//            vc.modalPresentationStyle = .fullScreen
//            self.present(vc, animated: true)
//            
//        }
    }
    
    @IBAction func cancel(_ sender: Any) {
        if let cancel = cancelBlock {
            cancel()
        }
        
        dismiss(animated: true, completion: nil)
    }
}
